<?php
class conciergeMod extends commonMod {
	public function __construct() {
		parent::__construct ();
		parent::UserIDIn ( $_GET ['_action'] );
	}
	public function get_concierge_list() {
		if ($this->_msg) {
			$this->respondError ( 201, $this->_msg );
			return;
		}
		
		$list = $this->model->table ( 'concierges' )->where ( "status  = 1" )->order ( 'id desc' )->select ();
		if ($list) {
			foreach ( $list as $k => $v ) {
				$list [$k] ['image'] = $v ['profile_image_url'];
				unset ( $list [$k] ['nickname'] );
				unset ( $list [$k] ['birthday'] );
				unset ( $list [$k] ['status'] );
				unset ( $list [$k] ['created'] );
				unset ( $list [$k] ['modified'] );
				unset ( $list [$k] ['profile_image_url'] );
			}
		}
		$this->respondSuccess ( $list, $_GET ['_action'] );
	}
	public function get_concierge() {
		if ($this->_msg) {
			$this->respondError ( 201, $this->_msg );
			return;
		}
		$info = $this->model->table ( 'concierges' )->where ( " id = " . $this->jsonData ['id'] )->find ();
		$info ['image'] = $info ['profile_image_url'];
		unset ( $info ['nickname'] );
		unset ( $info ['status'] );
		unset ( $info ['created'] );
		unset ( $info ['modified'] );
		unset ( $info ['profile_image_url'] );
		$this->respondSuccess ( $info, $_GET ['_action'] );
	}
	public function add_my_concierge() {
		if ($this->_msg) {
			$this->respondError ( 201, $this->_msg );
			return;
		}
		
		$check = $this->model->table ( 'concierges_users' )->where ( " user_id = " . $this->uid . " and concierge_id = " . $this->jsonData ['id'] )->find ();
		if ($check) {
			$this->respondError ( 201, "不能重复添加" );
			return;
		}
		
		$data ['user_id'] = $this->uid;
		$data ['concierge_id'] = $this->jsonData ['id'];
		$data ['status'] = 0;
		$data ['created'] = date ( "Y-m-d H:i:s" );
		$data ['modified'] = date ( "Y-m-d H:i:s" );
		$this->model->table ( 'concierges_users' )->data ( $data )->insert ();
		$this->respondSuccess ( null, $_GET ['_action'] );
	}
	public function remove_my_concierge() {
		if ($this->_msg) {
			$this->respondError ( 201, $this->_msg );
			return;
		}
		if (empty ( $this->jsonData ['id'] )) {
			$this->respondError ( 201, "缺少参数" );
			return;
		}
		$this->model->table ( 'concierges_users' )->where ( " user_id = " . $this->uid . " and concierge_id = " . $this->jsonData ['id'] )->delete ();
		$this->respondSuccess ( null, $_GET ['_action'] );
	}
	
	// 100
	// 邮件详情
	public function get_mail() {
		if ($this->_msg) {
			$this->respondError ( 201, $this->_msg );
			return;
		}
		$info = $this->model->table ( 'concierge_mails' )->where ( " id = " . $this->jsonData ['id'] )->find ();
		$users_info = $this->model->table ( 'users' )->where ( " id = " . $info ['user_id'] )->find ();
		if ($info ['direction'] == 1) {
			$concierges_info = $this->model->table ( 'concierges' )->where ( " id = " . $users_info ['concierge_id'] )->find ();
			$info ['nickname'] = $concierges_info ['nickname'];
			$info ['image'] = $concierges_info ['profile_image_url'];
			unset ( $concierges_info );
		}
		if ($info ['direction'] == 2) {
			$info ['nickname'] = $users_info ['nickname'];
			$info ['image'] = $users_info ['profile_image_url'];
			unset ( $users_info );
		}
		$info ['send_ts'] = $info ['created'];
		unset ( $info ['user_id'] );
		unset ( $info ['direction'] );
		unset ( $info ['created'] );
		unset ( $info ['modified'] );
		$this->respondSuccess ( $info, $_GET ['_action'] );
	}
	// 发送邮件
	public function send_mail() {
		if ($this->_msg) {
			$this->respondError ( 201, $this->_msg );
			return;
		}
		$data ['user_id'] = $this->uid;
		$data ['concierge_id'] = $this->jsonData ['concierge_id'];
		$data ['title'] = $this->jsonData ['title'];
		$data ['message'] = $this->jsonData ['message'];
		$data ['direction'] = $this->jsonData ['direction'];
		$data ['created'] = time ();
		$result = $this->model->table ( 'concierge_mails' )->data ( $data )->insert ();
		if ($result != false) {
			$result = true;
		}
		$this->respondSuccess ( $result, $_GET ['_action'] );
	}
	
	// 获取发件箱列表
	public function get_mail_send_list() {
		if ($this->_msg) {
			$this->respondError ( 201, $this->_msg );
			return;
		}
		$where = "user_id='" . $this->jsonData ['user_id'] . "'";
		if (! isset ( $this->jsonData ['concierge_id'] )) {
			$where .= " and concierge_id='" . $this->jsonData ['concierge_id'] . "'";
		}
		$list = $this->model->table ( 'concierge_mails' )->where ( $where )->limit ( $this->jsonData ['start_number'], $this->jsonData ['end_number'] )->select ();
		foreach ( $list as $key => $value ) {
			$concierges_info = $this->model->table ( 'concierges' )->where ( " id = " . $value ['concierge_id'] )->find ();
			$list [$key] ['nickname'] = $concierges_info ['nickname'];
			$list [$key] ['profile_image_url'] = $concierges_info ['profile_image_url'];
			$list [$key] ['send_ts'] = $concierges_info ['created'];
			unset ( $list [$key] ['message'] );
			unset ( $list [$key] ['modified'] );
			unset ( $list [$key] ['user_id'] );
			unset ( $list [$key] ['created'] );
			unset ( $concierges_info );
		}
		$data ['result'] = true;
		$data ['data'] = $list;
		$this->respondSuccess ( $data, $_GET ['_action'] );
	}
	
	// 获取收件箱列表
	public function get_mail_inbox_list() {
		if ($this->_msg) {
			$this->respondError ( 201, $this->_msg );
			return;
		}
		$where = "user_id='" . $this->jsonData ['user_id'] . "'";
		if (! isset ( $this->jsonData ['concierge_id'] )) {
			$where .= " and concierge_id='" . $this->jsonData ['concierge_id'] . "'";
		}
		$list = $this->model->table ( 'concierge_mails' )->where ( $where )->limit ( $this->jsonData ['start_number'], $this->jsonData ['end_number'] )->select ();
		foreach ( $list as $key => $value ) {
			$concierges_info = $this->model->table ( 'concierges' )->where ( " id = " . $value ['concierge_id'] )->find ();
			$list [$key] ['nickname'] = $concierges_info ['nickname'];
			$list [$key] ['profile_image_url'] = $concierges_info ['profile_image_url'];
			$list [$key] ['send_ts'] = $concierges_info ['created'];
			unset ( $list [$key] ['message'] );
			unset ( $list [$key] ['modified'] );
			unset ( $list [$key] ['user_id'] );
			unset ( $list [$key] ['created'] );
			unset ( $concierges_info );
		}
		$data ['result'] = true;
		$data ['data'] = $list;
		$this->respondSuccess ( $data, $_GET ['_action'] );
	}
}
?>